package android.app.gfestival.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.gfestival.soap.ArtistDescription;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.provider.BaseColumns;
import android.util.Log;

/**
 * A database helper to create the db table with country names
 * @author Eric
 *
 */
public class DbHelper extends SQLiteOpenHelper {

	public static final String TABLE_ARTIST = "artist";
	public static final String ARTIST_NAME = "name";
	public static final String TIME = "time";

	private static final String DATABASE_NAME = "artist.db";
	private static final int DATABASE_VERSION = 2;
	public static final String ARTIST_ID = "artist_id";
	public static final String ARTIST_SHORT_DES = "short_description";

	public DbHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("create table " + TABLE_ARTIST + " (" +
				BaseColumns._ID + " integer primary key autoincrement,"
				+ ARTIST_NAME + " text not null,"
				+ TIME + " text,"
				+ ARTIST_SHORT_DES+ " text,"
				+ ARTIST_ID + " text,"
				+ "unique (" + ARTIST_NAME + ") on conflict replace)");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.i("", "---------> drop table");
		db.execSQL("drop table " + TABLE_ARTIST);
		onCreate(db);
	}

	public int insertData(SQLiteDatabase db, List<ArtistDescription> artists){
		int numInserted = 0;
		db.beginTransaction();
		try {
			int length = artists.size();
			for(int i=0;i<length;i++){
				ArtistDescription a = artists.get(i);
				Log.i("", "--------->" + ARTIST_ID + ": " + a.artist_id);
				ContentValues values = new ContentValues();
				values.put(ARTIST_NAME, a.artist_name);
				values.put(ARTIST_ID, a.artist_id);
				values.put(ARTIST_SHORT_DES, a.artist_short_description);
				values.put(TIME, a.artist_time);
				long effec = db.insert(TABLE_ARTIST, null, values);
				Log.i("DbHelper", "--->insert: " + effec);
			}
			db.setTransactionSuccessful();
			numInserted = length;
		}catch (Exception e) {
			e.printStackTrace();
		} finally {
			db.endTransaction();
		}
		return numInserted;
	}
	
	private int putData(SQLiteDatabase db,ArrayList<Artist> artists){
		int numInserted = 0;
		db.beginTransaction();
		try {
			SQLiteStatement insert = db.compileStatement("insert into " +
					TABLE_ARTIST + "(" + ARTIST_NAME + "," + TIME +")"
					+ "values " + "(?,?)");
			for (Artist ars : artists){
				insert.bindString(1, ars.name);
				insert.bindString(2, ars.time);
				insert.execute();
			}
			db.setTransactionSuccessful();
			numInserted = artists.size();
		} finally {
			db.endTransaction();
		}
		return numInserted;
	}
}